<template>
  <div class="contentBox">
    <h2 class="title">
      <span
        >{{ isMy ? "创建的歌单" : "收藏的歌单" }}({{ dataArr.length }})</span
      >
      <el-button
        size="mini"
        class="addBtn"
        v-if="isMy"
        @click="showOrhideSongSheetBox('show')"
        >新建</el-button
      >
    </h2>
    <ul>
      <li
        @click="jumpUrl(item.id, item.name)"
        class="itemBox"
        v-for="(item, index) in dataArr"
        :key="item.id"
      >
        <img :src="item.coverImgUrl" class="picImg" alt="" />
        <div class="itemRight">
          <h3 class="itemName txtHide">{{ item.name }}</h3>
          <span class="itemTe"> {{ item.trackCount }}首歌</span>
          <!-- 是用户创建的，执行删除 -->
          <i
            class="iconfont icon-huishouzhan_huaban myicon"
            @click.stop="delMysongSheet(item.id)"
            v-if="isMy && index != 0"
          ></i>
          <!-- 不是用户创建的，执行取关 -->
          <i
            class="iconfont icon-huishouzhan_huaban myicon"
            @click.stop="passMysongSheet(item.id)"
            v-if="!isMy"
          ></i>
          <!-- 设置 -->
          <i
            class="iconfont icon-shezhi myicon"
            v-if="isMy"
            @click.stop="getSongSheetDetail(item.id)"
          ></i>
        </div>
      </li>
    </ul>
    <!-- 设置歌单 -->
    <div class="te">
      <el-drawer
        :title="songDetail && songDetail.name"
        :visible.sync="drawer"
        direction="rtl"
      >
        <ul>
          <li
            class="songItem"
            v-for="item in songDetail && songDetail.tracks"
            :key="item.id"
          >
            <img class="itemPic" :src="item.al.picUrl" alt="" />
            <p class="songName txtHide" :title="item.name">{{ item.name }}</p>
            <i
              class="iconfont icon-x"
              @click="delsong(item.id, songDetail.id)"
            ></i>
          </li>
        </ul>
      </el-drawer>
    </div>

    <div
      class="AddCollectionBox"
      v-drag="'dragId'"
      v-loading="addloading"
      element-loading-text="请稍后"
      element-loading-spinner="el-icon-loading"
      v-if="songSheetNameBox"
    >
      <div class="collectionTit" id="dragId">
        <p class="collectionTxt">新建歌单</p>
        <i
          class="iconfont icon-x hideBtn"
          @click="showOrhideSongSheetBox('hide')"
        ></i>
      </div>
      <div class="addInputBox">
        <el-form label-width="80px">
          <el-form-item label="歌单名">
            <input v-model="songSheetName" type="text" class="songSheetName" />
          </el-form-item>
        </el-form>
        <p class="notes">可通过“收藏”将音乐添加到新歌单中</p>
        <div class="btnBox">
          <div class="btnClass yesBtn" @click="addSongSheetFn">
            <i>确 定</i>
          </div>
          <div class="btnClass noBtn" @click="showOrhideSongSheetBox('hide')">
            <i>取 消</i>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Notification } from "element-ui";
import {
  userSongSheet,
  delPlaylist,
  PassPlaylist,
  songSheetDetail,
  collectionMusic,
  addSongSheet,
} from "@/api";
export default {
  props: {
    uid: {
      type: Number,
      default: 0,
    },
    isMy: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      dataArr: [], //歌单数据
      drawer: false, //右侧的歌单详情显示状态
      songDetail: null, //选择的歌单详情
      songSheetNameBox: false, //新增歌单框的显示和隐藏
      songSheetName: "", //歌单名
      addloading: false, //loading状态
    };
  },
  created() {
    this.getUserData();
  },
  methods: {
    //获取数据
    getUserData() {
      userSongSheet({ uid: this.uid, limit: 9999 }).then((res) => {
        let arr = [];
        res.playlist.forEach((item) => {
          //item.subscribed是否是用户创建的
          if (!item.subscribed === this.isMy) arr.push(item);
        });
        this.dataArr = arr;
      });
    },
    //跳转连接
    jumpUrl(id) {
      let { href } = this.$router.resolve({
        name: "songsheetdetail",
        query: { id },
      });
      window.open(href, "_blank");
    },
    //删除歌单
    delMysongSheet(id) {
      this.$confirm("此操作将永久删除该歌单, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delPlaylist({ id }).then((res) => {
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getUserData();
          }
        });
      });
    },
    //取关歌单
    passMysongSheet(id) {
      this.$confirm("此操作将取消收藏该歌单, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        PassPlaylist({ t: 2, id }).then((res) => {
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: "取消收藏成功!",
            });
            this.getUserData();
          }
        });
      });
    },
    //获取歌单详情
    getSongSheetDetail(id) {
      songSheetDetail({ id }).then((res) => {
        this.drawer = true;
        this.songDetail = res.playlist;
      });
    },
    //删除音乐
    delsong(id, pid) {
      this.$confirm("此操作将取消收藏该歌曲, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        collectionMusic({ op: "del", tracks: id, pid }).then((res) => {
          this.$message({
            type: "success",
            message: "取消收藏成功!",
          });
          console.log(res);
          this.songDetail.tracks.forEach((item, index) => {
            if (item.id === id) this.songDetail.tracks.splice(index, 1);
          });
        });
      });
    },
    //新建歌单
    addSongSheetFn() {
      if (this.songSheetName.trim() == "") return;
      this.addloading = true;
      addSongSheet({ name: this.songSheetName }).then((res) => {
        this.addloading = false;
        if (res.code == 200) {
          Notification({
            message: "歌单新建成功",
            type: "success",
            duration: 1500,
          });
          this.getUserData();
          this.showOrhideSongSheetBox("hide");
          this.songSheetName = "";
        }
      });
    },
    //显示歌单新增框
    showOrhideSongSheetBox(type) {
      if (type == "show") {
        this.songSheetNameBox = true;
      } else {
        this.songSheetNameBox = false;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.contentBox {
  padding-top: 40px;
  .title {
    font-size: 24px;
    font-weight: normal;
    height: 40px;
    border-bottom: 2px solid #c20c0c;
    .addBtn {
      float: right;
    }
  }
  .itemBox {
    display: flex;
    padding: 10px 0;
    border-bottom: 1px solid #ddd;
    .picImg {
      margin-right: 15px;
      width: 50px;
      height: 50px;
    }
    .itemRight {
      overflow: hidden;
      flex: 1;
      .itemName {
        color: #333;
        font-size: 14px;
        line-height: 20px;
      }
      .itemTe {
        color: #666;
        line-height: 20px;
        margin-right: 15px;
      }
      .myicon {
        float: right;
        margin-right: 10px;
      }
    }
  }
}
/deep/ :focus {
  outline: 0;
}
/deep/ .el-drawer__body {
  overflow: auto;
}
.songItem {
  padding: 10px;
  border-bottom: 1px solid #ccc;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .songName {
    flex: 1;
    padding: 0 10px;
  }
  .itemPic {
    width: 40px;
    height: 40px;
  }
}
.AddCollectionBox {
  position: fixed;
  left: calc(50% - 265px);
  top: calc(50% - 265px);
  background: #fff;
  z-index: 99999;
  width: 480px;
  display: flex;
  flex-direction: column;
  height: 250px;
  border-radius: 4px;
  box-shadow: 0 5px 16px rgba(0, 0, 0, 0.8);
  .collectionTit {
    background: #2d2d2d;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .collectionTxt {
      padding-right: 45px;
      margin: 0;
      height: 38px;
      line-height: 38px;
      padding-left: 18px;
      border-radius: 3px 3px 0 0;
      font-weight: bold;
      font-size: 14px;
      color: #fff;
    }
    .hideBtn {
      color: #fff;
      display: block;
      padding: 10px;
    }
  }
  .addInputBox {
    padding: 40px 0;
    margin-left: 30px;
    text-align: left;
    .songSheetName {
      height: 19px;
      margin: 0;
      padding: 5px 6px 6px;
      border: 1px solid #cdcdcd;
      border-radius: 2px;
      line-height: 19px;
      box-sizing: content-box;
      vertical-align: middle;
      width: 300px;
      outline: none;
    }
    .notes {
      margin: 8px 0 20px 48px;
      color: #999;
    }
    .btnBox {
      margin-left: 48px;
      .btnClass {
        color: #333;
        background: url("./../../assets/imges/backgroundStripe.png") no-repeat;
        padding: 0 5px 0 0;
        white-space: nowrap;
        display: inline-block;
        vertical-align: middle;
        margin-right: 5px;
        i {
          padding: 0 15px 0 20px;
          height: 31px;
          line-height: 30px;
          display: inline-block;
          min-width: 23px;
          background: url("./../../assets/imges/backgroundStripe.png") no-repeat;
          cursor: pointer;
        }
      }
      .yesBtn {
        background-position: right -428px;
        color: #fff;
        i {
          background-position: 0 -387px;
        }
      }
      .noBtn {
        background-position: right -100px;
        margin-left: 20px;
        i {
          background-position: 0 -59px;
        }
      }
    }
  }
}
</style>